import generatedRoutes from 'virtual:generated-pages'
import store from '@/store'

const FOLD_MENU = ['Others']

export default [
  {
    name: 'Dashboard',
    path: '/',
    component: () => import('@/layout/TopNavi.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/pages/Management/Dashboard.vue')
      }
    ],
    meta: {
      inNavigation: true,
      icon: 'Others'
    }
  },
  {
    path: '/sales-settings',
    name: 'Sales Settings',
    component: () => import('@/layout/TopNavi.vue'),
    redirect: () => {
      // 找到有权限的第一个子路由
      const children = generatedRoutes
        .filter(route => route.path.match(/^\/sales-settings\//))
        .sort((a, b) => a.meta.sort - b.meta.sort)
      for (const route of children) {
        if (route.meta.roles && typeof route.meta.roles.split === 'function') {
          const roles = route.meta.roles.split(',')
          if (roles.some(role => store.getters['user/' + role.trim()] === true)) {
            return route.path
          }
        }
      }
      return '/404'
    },
    children: generatedRoutes
      .filter(route => route.path.match(/^\/sales-settings\//))
      .sort((a, b) => a.meta.sort - b.meta.sort),
    meta: {
      inNavigation: true,
      icon: 'Sales_Settings',
      roles: 'isPrimaryCSO, isBUM, isSalesSupport'
    }
  },
  {
    path: '/phase-management-m',
    name: 'Phase Management',
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes.filter(route => route.path.match(/^\/phase-management-m\//)),
    meta: {
      inNavigation: true,
      icon: 'PhaseManagement',
      roles: 'isPrimaryCSO, isBranchCSO, isBUM'
    }
  },
  {
    path: '/target-clients-m',
    name: 'Target Clients',
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes.filter(route => route.path.match(/^\/target-clients-m\//)),
    meta: {
      inNavigation: true,
      icon: 'Target_Clients',
      roles: 'isPrimaryCSO, isBranchCSO, isBUM, isSalesSupport'
    }
  },
  {
    path: '/existing-clients-m',
    name: 'Existing Clients',
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes.filter(route => route.path.match(/^\/existing-clients-m\//)),
    meta: {
      inNavigation: true,
      icon: 'Existing_Clients',
      roles: 'isPrimaryCSO, isBranchCSO, isBUM, isSalesSupport'
    }
  },
  {
    name: 'Organization',
    path: '/organization',
    component: () => import('@/layout/TopNavi.vue'),
    redirect: '/organization/organization-view',
    children: generatedRoutes
      .filter(route => route.path.match(/^\/organization\//))
      .sort((a, b) => a.meta.sort - b.meta.sort),
    meta: {
      inNavigation: true,
      foldInto: FOLD_MENU[0],
      icon: 'Organization',
      roles: 'isPrimaryCSO, isSalesSupport'
    }
  },
  {
    name: 'CRM Operate Record',
    path: '/crm-operate-record',
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes.filter(route => route.path.match(/^\/crm-operate-record\//)),
    meta: {
      inNavigation: true,
      foldInto: FOLD_MENU[0],
      icon: 'Organization',
      roles: 'isPrimaryCSO, isSalesSupport'
    }
  },
  {
    name: 'Sales Division',
    path: '/sales-division',
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes.filter(route => route.path.match(/^\/sales-division\//)),
    meta: {
      inNavigation: true,
      foldInto: FOLD_MENU[0],
      icon: 'Others',
      roles: 'isPrimaryCSO, isBranchCSO'
    }
  },
  {
    name: 'Client Pool',
    path: '/pool-m',
    component: () => import('@/layout/TopNavi.vue'),
    redirect: '/pool-m/client-pool',
    children: generatedRoutes
      .filter(route => route.path.match(/^\/pool-m\//))
      .sort((a, b) => a.meta.sort - b.meta.sort),
    meta: {
      inNavigation: true,
      foldInto: FOLD_MENU[0],
      icon: 'Pool',
      roles: 'isPrimaryCSO, isBranchCSO, isBUM, isSalesSupport, isBusinessPersonnel'
    }
  },
  {
    name: 'Clients',
    path: '/clients-m',
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes.filter(route => route.path.match(/^\/clients-m\//)),
    meta: {
      inNavigation: true,
      foldInto: FOLD_MENU[0],
      icon: 'Others',
      roles: 'isPrimaryCSO, isBranchCSO'
    }
  },
  {
    name: 'AM & PM',
    path: '/am-pm',
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes.filter(route => route.path.match(/^\/am-pm\//)),
    meta: {
      inNavigation: true,
      foldInto: FOLD_MENU[0],
      icon: 'Others',
      roles: 'isPrimaryCSO'
    }
  },
  {
    name: 'Blacklist',
    path: '/blacklist',
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes.filter(route => route.path.match(/^\/blacklist\//)),
    meta: {
      inNavigation: true,
      foldInto: FOLD_MENU[0],
      icon: 'Others',
      roles: 'isPrimaryCSO'
    }
  },
  {
    name: 'Project List',
    path: '/project-list',
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes.filter(route => route.path.match(/^\/project-list\//)),
    meta: {
      inNavigation: true,
      foldInto: FOLD_MENU[0],
      icon: 'Others',
      roles: 'isSalesSupport'
    }
  },
  {
    path: '/statistics-m',
    name: 'Statistics',
    redirect: '/statistics-m/invoice-number',
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes
      .filter(route => route.path.match(/^\/statistics-m\//))
      .sort((a, b) => a.meta.sort - b.meta.sort),
    meta: {
      inNavigation: true,
      icon: 'Others',
      foldInto: FOLD_MENU[0],
      roles: 'isPrimaryCSO, isBranchCSO'
    }
  },
  {
    path: '/statistics-bu',
    name: 'BU Statistics',
    redirect: () => {
      // 找到有权限的第一个子路由
      const children = generatedRoutes
        .filter(route => route.path.match(/^\/statistics-bu\//))
        .sort((a, b) => a.meta.sort - b.meta.sort)
      for (const route of children) {
        if (route.meta.roles && typeof route.meta.roles.split === 'function') {
          const roles = route.meta.roles.split(',')
          if (roles.some(role => store.getters['user/' + role.trim()] === true)) {
            return route.path
          }
        }
      }
      return '/404'
    },
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes
      .filter(route => route.path.match(/^\/statistics-bu\//))
      .sort((a, b) => a.meta.sort - b.meta.sort),
    meta: {
      inNavigation: true,
      icon: 'Others',
      foldInto: FOLD_MENU[0],
      roles: 'isBUM, isHRBP'
    }
  },
  {
    name: 'Pending Clients',
    path: '/pending-clients',
    redirect: () => {
      // 找到有权限的第一个子路由
      const children = generatedRoutes
        .filter(route => route.path.match(/^\/pending-clients\//))
        .sort((a, b) => a.meta.sort - b.meta.sort)
      for (const route of children) {
        if (route.meta.roles && typeof route.meta.roles.split === 'function') {
          const roles = route.meta.roles.split(',')
          if (roles.some(role => store.getters['user/' + role.trim()] === true)) {
            return route.path
          }
        }
      }
      return '/404'
    },
    component: () => import('@/layout/TopNavi.vue'),
    children: generatedRoutes
      .filter(route => route.path.match(/^\/pending-clients\//))
      .sort((a, b) => a.meta.sort - b.meta.sort),
    meta: {
      inNavigation: true,
      foldInto: FOLD_MENU[0],
      icon: 'Target_Clients',
      roles: 'isBUM, isPrimaryCSO, isSalesSupport'
    }
  }
]
